package cn.itcast.foodapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaParser;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import cn.itcast.foodapp2.bean.LoginBean;
import cn.itcast.foodapp2.net.NetUtil;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";
    EditText mEtTelephone, mEtPassword;
    Button mBtnLogin, mBtnCancel;
    TextView mTvRegister, mTvForgetPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initEvent();
    }

    private void initEvent() {
        mTvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String telephone = mEtTelephone.getText().toString();
                final String password = mEtPassword.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("telephone", telephone);
                            jsonObject.put("password", password);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String url = "http://42.48.39.55:8081/foodserver/user/login";
                        //10.2.64.75：8080/foodserver/user/login
                        String data = NetUtil.getData(jsonObject, url);

                        Gson gson = new Gson();
                        final LoginBean loginBean = gson.fromJson(data, LoginBean.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (loginBean.isSuccess()) {
                                    Intent intent = new Intent(LoginActivity.this,
                                            FoodListActivity.class);
                                    //TODO 获取用户ID,传递给下一个Activity
                                    int id = loginBean.getData().getId();
                                    // intent.putExtra("userId", id);
                                    // 将userId
                                    SharedPreferences sp = getSharedPreferences("data.sp", MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putInt("userId", id);
                                    edit.commit();

                                    startActivity(intent);
                                } else {
                                    Toast.makeText(LoginActivity.this,
                                            "用户名密码错误！",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        });


                    }
                }).start();
            }
        });
    }


    private void initView() {
        mEtTelephone = findViewById(R.id.et_telephone);
        mEtPassword = findViewById(R.id.et_password);
        mBtnLogin = findViewById(R.id.btn_login);
        mBtnCancel = findViewById(R.id.btn_cancel);
        mTvRegister = findViewById(R.id.tv_register);
        mTvForgetPassword = findViewById(R.id.tv_forget_password);
    }
}